<template>
  <div>
    <!--面包屑-->
    <breadcrumb></breadcrumb>
    <!-- 卡片-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <search :list="collegeList" :type="'college'" @clearSearch="clearSearch" @searchData="searchData"></search>
        <el-col :span="3"></el-col>
      </el-row>
      <!-- 用户列表-->
      <el-table :data="dataList" border stripe>
        <el-table-column prop="collegeId" label="版块号"></el-table-column>
        <el-table-column prop="collegeName" label="版块名"></el-table-column>
        <el-table-column prop="moderator" label="版主"></el-table-column>
        <el-table-column prop="postCount" label="帖子量"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="success" icon="el-icon-edit" size="mini" @click="edit(scope.row)"></el-button>
            <!--删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deletes(scope.row.collegeId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--修改版块对话框-->
    <el-dialog
      :title="'修改版块:'+collegeDialogName"
      :visible.sync="editDialogVisiable"
      @open="openEdit()"
      @close="closeEdit()"
      width="50%"
      class="add_dialog"
    >
      <!--内容主体-->
      <edit :scope="scope" v-if="isShowEdit" @editSuccess="editSuccess"></edit>
    </el-dialog>
  </div>
</template>
<script>
import breadcrumb from '../breadcrumb.vue'
import search from '../search.vue'
import edit from './edit.vue'
export default {
  data() {
    return {
      collegeList: [],
      dataList: [],
      scope: '',
      addDialogVisiable: false, // 添加版块对话框
      collegeDialogName: '',
      isShowEdit: false, // 显示修改组件
      editDialogVisiable: false // 修改版块对话框
    }
  },
  created() {
    this.comm.getcollege().then(async data => {
      data.forEach(async item => {
        await this.getpostinfo(item.collegeId).then(res => {
          item.postCount = res.length
        })
        this.collegeList.push(item)
        this.dataList.push(item)
      })
    })
  },
  methods: {
    async getpostinfo(id) {
      const postRes = await this.$http.get('/getpostinfo/' + id)

      if (postRes.meta.status !== 200) {
        return this.$Message.error('获取帖子信息失败')
      }
      return postRes.data
    },
    searchData(data) {
      this.dataList = data
    },
    clearSearch() {
      this.dataList = this.collegeList
    },
    // 编辑信息
    edit(scope) {
      this.scope = scope
      this.collegeDialogName = scope.collegeName
      this.editDialogVisiable = true
    },
    openEdit() {
      this.isShowEdit = true
    },
    closeEdit() {
      this.isShowEdit = false
    },
    editSuccess(data) {
      // 编辑成功后
      this.editDialogVisiable = false
      this.$router.go(0)
    },
    closeAdd() {},
    deletes(scope) {
      // 删除
      this.comm.deletes(scope, '/getcollege/', this).then(res => {
        if (!res) {
          return
        }
        this.comm.ArrSplice(this.dataList, 'collegeId', scope)
      })
    }
  },
  components: {
    breadcrumb,
    search,
    edit
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 10px;
}
.el-table {
  margin-top: 15px;
}
.el-divider--horizontal {
  margin: 10px 0;
}
</style>
